{extend name="layout:base" /}
{block name="page_content"}

  <form class="form-horizontal adminform" id="model-form" method="post" action="">
    <div class="form-group">
      <label class="col-sm-2 control-label no-padding-right" for="form-field-1">
        管理员组名：<span class="red">*</span>
      </label>
      <div class="col-sm-10">
        <input type="text" id="title" name="title" placeholder="请输入菜单名称" class="col-xs-10 col-sm-5" value="{$group.title|default=''}" required/>
      </div>
    </div>
    <div class="space-4"></div>

    <div class="form-group">
      <label class="col-sm-2 control-label no-padding-right" for="form-field-1"> 显示状态：  </label>
      <div class="col-sm-10" style="padding-top:5px;">
        <input name="status" value="1" class="ace ace-switch ace-switch-4 btn-flat" type="checkbox" <?php if(isset($group["status"]) && intval($group["status"])){echo('checked="checked"');} ?> />
        <span class="lbl"></span>
      </div>
    </div>
    <div class="space-4"></div>

    <div class="form-group">
      <label class="col-sm-2 control-label no-padding-right" for="form-field-1"> 权限选择：  </label>
      <div class="col-sm-9">
        {volist name="all_menu_tree" id="v"}
          <div class="row">
            <div class="widget-box">
              <div class="widget-header">
                <h4 class="widget-title">
                  <label>
                    <input name="rules[]" class="ace ace-checkbox-2 father" type="checkbox" value="{$v['id']}" <?php if(in_array($v['id'], $group_rules_id)){echo('checked="checked"');} ?> onchange="changeGroupStatus(this);"/>
                    <span class="lbl"> {$v['title']}</span>
                  </label>
                </h4>
                <div class="widget-toolbar">
                  {notempty name="v.children"}
                    <a href="#" data-action="collapse">
                      <i class="ace-icon fa fa-chevron-up"></i>
                    </a>
                  {/notempty}
                </div>
              </div>
              {notempty name="v.children"}
                <div class="widget-body">
                  <div class="widget-main row">
                    {volist name="v.children" id="vv"}
                      <label class="col-xs-2" style="width:160px;">
                        <input name="rules[]" class="ace ace-checkbox-2 children" type="checkbox" value="{$vv['id']}" <?php if(in_array($vv['id'], $group_rules_id)){echo('checked="checked"');} ?>/>
                        <span class="lbl"> {$vv['title']}</span>
                      </label>
                      {volist name="vv.children" id="vvv"}
                        <label class="col-xs-2" style="width:160px;">
                          <input name="rules[]" class="ace ace-checkbox-2 children" type="checkbox" value="{$vvv['id']}" <?php if(in_array($vvv['id'], $group_rules_id)){echo('checked="checked"');} ?>/>
                          <span class="lbl"> {$vvv['title']}</span>
                        </label>
                      {/volist}
                    {/volist}
                  </div>
                </div>
              {/notempty}
            </div>
          </div>
        {/volist}
      </div>
    </div>
    <div class="space-4"></div>

    <div class="clearfix form-actions">
      <div class="col-md-offset-3 col-md-9">
        <?php if(isset($group['id'])): ?>
          <input type="hidden" name="id" value="{$group.id}">
        <?php endif; ?>
        <input type="hidden" name="check_id" id="check_id" value="{$group.id|default=''}">
        <button class="btn btn-info" type="submit">
          <i class="ace-icon fa fa-check bigger-110"></i>
          保存
        </button>

        &nbsp; &nbsp; &nbsp;
        <a class="btn" href="{:url('index')}">
          <i class="ace-icon fa fa-undo bigger-110"></i>
          返回
        </a>
        <div class="text-danger" style="margin-top: 10px;">添加或修改后菜单后，管理员用户需要重新登录才能看到权限和左侧菜单变化。</div>
      </div>
    </div>
  </form>
{/block}
{block name="page_js"}
  <script src="/static/plugins/jquery-validate/jquery.validate.min.js"></script>
  <script type="text/javascript">
    function changeGroupStatus(clickItem){
      var $checkItem = $(clickItem);
      var $widgetBox = $checkItem.parents("div.widget-box");
      var $checkboxChildren = $widgetBox.find('div.widget-body input.ace-checkbox-2.children');
      $checkboxChildren.each(function(){
        $(this).prop("checked", $checkItem.is(':checked'));
      });
    }

    $(document).ready(function(){
      $("form#model-form").validate({
        rules:{
          "title":{
            required: true,
            minlength: 1,
            maxlength: 30,
            remote: {
              url: "{:url('ajax_check_unique')}",
              type: "post",
              data: {
                id: function() {
                  return $("#check_id").val();
                },
                field: 'title'
              }
            }
          }
        },
        messages:{
          "title":{
            required: "管理员组名不能为空",
            minlength: "管理员组名不能少于1个字符",
            maxlength: "管理员组名不能超过30个字符",
            remote: "管理员组名已存在"
          }
        },
        errorClass: "help-inline input-error",
        errorElement: "span",
        highlight:function(element, errorClass, validClass) {
          $(element).parents('.form-group').addClass('has-error');
        },
        unhighlight: function(element, errorClass, validClass) {
          $(element).parents('.form-group').removeClass('has-error');
        },
        submitHandler: function(form){
          var $form = $(form);
          $.ajax({
            url: $form.attr("action"),
            type: $form.attr("method"),
            dataType: "JSON",
            data: $form.serialize(),
            success: function(data){
              if(data.code){
                location.href = data["url"];
              }else{
                alert(data["msg"]);
              }
            }, error: function () {
              alert("数据执行错误！");
            }
          });
        }
      });
    });
  </script>
{/block}